package com.wanhe.shoppingcart.cuiyu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanhe.base.model.base.DishTag;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.base.util.StringFormatUtil;
import com.wanhe.shoppingcart.IShowDialogView;
import com.wanhe.shoppingcart.R;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.shoppingcart.manager.ICartItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XieZaiWei on 2017/5/22.
 * 脆鱼购物车菜品子项list布局样式的Adapter
 */

public class CuiYuListAdapter extends BaseAdapter implements IShowDialogView {
    private LayoutInflater mInflater;
    private ArrayList<CartItem> mArrCartItem;
    private int mItemLayout;


    public CuiYuListAdapter(Context context, ArrayList<CartItem> arrCartItem, int itemLayout) {
        mInflater = LayoutInflater.from(context);
        mArrCartItem = arrCartItem;
        mItemLayout = itemLayout;
    }

    @Override
    public int getCount() {
        return mArrCartItem.size();
    }

    @Override
    public ICartItem getItem(int position) {
        return mArrCartItem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(mItemLayout, null);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.iv_plus = (ImageView) convertView.findViewById(R.id.iv_plus);
            viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            viewHolder.iv_reduce = (ImageView) convertView.findViewById(R.id.iv_reduce);
            viewHolder.tv_taste = (TextView) convertView.findViewById(R.id.tv_taste);
            viewHolder.tv_garnish = (TextView) convertView.findViewById(R.id.tv_garnish);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final CartItem cartItem = mArrCartItem.get(position);
        List<DishTag> arrTag = cartItem.getTags();
        if (arrTag != null && arrTag.size() > 0) {
            String tags = "";
            for (int i = 0; i < arrTag.size(); i++) {
                String name = arrTag.get(i).getName();
                tags += name + " ";
            }
            viewHolder.tv_taste.setText("口味：" + tags);

        } else {
            viewHolder.tv_taste.setText("口味：无");
        }

        List<CartItem> arrItem = ((CuiYuPot) cartItem).getArrChildItem();
        if (arrItem != null && arrItem.size() > 0) {
            String name = "";
            for (int i = 0; i < arrItem.size(); i++) {
                String garnish = arrItem.get(i).getName();
                garnish = StringFormatUtil.cuiYuGarnishName(garnish);
                name += garnish + " ";
            }
            viewHolder.tv_garnish.setText("伴料：" + name);
        } else {
            viewHolder.tv_garnish.setText("伴料：无");
        }

        String potName = cartItem.getName();
        switch (potName) {
            case "(酱香)金椒龙鱼拼牛":
            case "(酱香)金椒脆鱼拼牛":
            case "(酱香)金椒乌鱼拼牛":
                potName = StringFormatUtil.cuiYuName(cartItem.getName());
                break;

            default:
                break;
        }
        if (ObjectUtil.checkObject(cartItem.getSize())) {
            viewHolder.tv_name.setText(potName + "(" + cartItem.getSize().getName() + ")");

        } else {
            viewHolder.tv_name.setText(potName);
        }

        viewHolder.tv_price.setText("￥" + cartItem.getPrice());
        viewHolder.tv_count.setText(String.valueOf(cartItem.getCount()));
        viewHolder.iv_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int step = CartManager.getInstance().getAddStep(cartItem.getDishGuid());
                if(step != 0){
                    CartManager.getInstance().addItem(cartItem.getDishGuid(), step);
                }
                notifyDataSetChanged();
            }
        });

        viewHolder.iv_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int step = CartManager.getInstance().getReduceStep(cartItem.getDishGuid());
                if(step != 0){
                    CartManager.getInstance().reduceItem(cartItem.getDishGuid(), step);
                }
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        public TextView tv_name;
        public TextView tv_price;
        public ImageView iv_plus;
        public TextView tv_count;
        public ImageView iv_reduce;
        public TextView tv_taste;
        public TextView tv_garnish;
    }

    @Override
    public void showSetMealDialog(String dishID) {

    }

    @Override
    public void showNormalDialog(String dishID) {

    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onLoaded() {

    }

    @Override
    public void onLoadFail(String msg) {

    }
}
